package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQueryObject;
import cn.wolfcode.car.business.service.impl.StatementItemServiceImpl;
import cn.wolfcode.car.business.service.impl.StatementServiceImpl;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 声明项控制器
 * 控制器
 *
 * @author RNIZ
 * @date 2021/11/15
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    @Autowired
    private StatementServiceImpl statementService;
    @Autowired
    private StatementItemServiceImpl statementItemService;
    @Autowired
    private IUserService userService;
    private static final String PREFIX = "business/statementItem/";

    @RequestMapping("/itemDetail")
    private String itemDetail(Long statementId, Model model) {
        //根据结算单id查找支付状态
        Statement statement = statementService.get(statementId);
            model.addAttribute("statement", statement);
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            ///消费中
            return PREFIX + "itemAdd";
        } else {
            Long payeeId = statement.getPayeeId();
            User user = userService.get(payeeId);
            statement.setPayee(user);
            return PREFIX + "itemDetail";
        }
    }
    /**数据
     列表*/
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQueryObject qo){
        return statementItemService.query(qo);
    }


    /**
     * 保存项目
     *
     * @param items 项目
     * @return {@link AjaxResult}
     */
    @RequestMapping("/saveItems")
    @ResponseBody
    private AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequestMapping("/payStatement")
    @ResponseBody
    private AjaxResult payStatement(Long statementId) {
        statementService.changeBystatus(statementId);
        return AjaxResult.success();
    }


}
